package api

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"

	"go-caipu/pkg/api/resp"
	"go-caipu/pkg/api/web"
	"go-caipu/pkg/services/{{.PackageName}}/{{.ModuleName}}"
)

func (hs *HTTPServer) Get{{.ClassName}}Page(c *gin.Context) resp.Response {
	cmd := {{.ModuleName}}.Get{{.ClassName}}PageCommand{}
	if err := web.Bind(c, &cmd); err != nil {
		return resp.ErrParam.WithErr(err)
	}
	result, count, err := hs.{{.ModuleName}}Service.Get{{.ClassName}}Page(c.Request.Context(), cmd)
	if err != nil {
		return resp.ErrPostService.WithErr(err)
	}
	return resp.OK.WithData(resp.Page{CurrentPage: cmd.PageIndex, PageSize: cmd.PageSize, List: result, Total: count})
}
func (hs *HTTPServer) Get{{.ClassName}}(c *gin.Context) resp.Response {
	cmd := {{.ModuleName}}.Get{{.ClassName}}Command{}
	if err := web.Bind(c, &cmd); err != nil {
		return resp.ErrParam.WithErr(err)
	}
	result, err := hs.{{.ModuleName}}Service.Get(c.Request.Context(), cmd)
	if err != nil {
		return resp.ErrPostService.WithErr(err)
	}
	return resp.OK.WithData(result)

}
func (hs *HTTPServer) Create{{.ClassName}}(c *gin.Context) resp.Response {
	cmd := {{.ModuleName}}.{{.ClassName}}{}
	if err := web.Bind(c, &cmd, binding.JSON); err != nil {
		return resp.ErrParam.WithErr(err)
	}
	err := hs.{{.ModuleName}}Service.Insert(c.Request.Context(), cmd)
	if err != nil {
		return resp.ErrPostService.WithErr(err)
	}
	return resp.OK
}

func (hs *HTTPServer) Update{{.ClassName}}(c *gin.Context) resp.Response {
	cmd := {{.ModuleName}}.{{.ClassName}}{}
	if err := web.Bind(c, &cmd, binding.JSON); err != nil {
		return resp.ErrParam.WithErr(err)
	}
	err := hs.{{.ModuleName}}Service.Update(c.Request.Context(), cmd)
	if err != nil {
		return resp.ErrPostService.WithErr(err)
	}
	return resp.OK
}

func (hs *HTTPServer) Remove{{.ClassName}}(c *gin.Context) resp.Response {
	cmd := {{.ModuleName}}.Remove{{.ClassName}}Command{}
	if err := web.Bind(c, &cmd, binding.JSON); err != nil {
		return resp.ErrParam.WithErr(err)
	}
	err := hs.{{.ModuleName}}Service.Remove(c.Request.Context(), cmd)
	if err != nil {
		return resp.ErrPostService.WithErr(err)
	}
	return resp.OK
}
